package com.hnyyac.ad.mds.views_notload

import android.annotation.SuppressLint
import android.app.Activity
import android.util.Log
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.CSJAdError
import com.bytedance.sdk.openadsdk.CSJSplashAd
import com.bytedance.sdk.openadsdk.TTAdNative.CSJSplashAdListener
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.mediation.MediationConstant
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot
import com.bytedance.sdk.openadsdk.mediation.ad.MediationSplashRequestInfo
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager


class NewGromoreSplashAd(private val activity: Activity, private val listener: IAdviewNotLoadSplashListener) {
    private val TAG = "qin"
    private var mSplashAd: CSJSplashAd? = null
    private lateinit var MyadUnitId :String

    @SuppressLint("SuspiciousIndentation")
    fun load(adUnitId: String, container: ViewGroup, time:Long){
        MyadUnitId=adUnitId;
        //第一步、创建开屏自定义兜底对象
        val csjSplashRequestInfo: MediationSplashRequestInfo = object : MediationSplashRequestInfo(
            MediationConstant.ADN_PANGLE,  // 穿山甲
            "103395885",  // adn开屏广告代码位Id，注意不是聚合广告位Id
            "5669830",  // adn应用id，注意要跟初始化传入的保持一致
            "" // adn没有appKey时，传入空即可
        ) {}
        //step1
        val  adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity)
        //step2
        val  adSlot = AdSlot.Builder()
            .setCodeId(adUnitId)
            .setImageAcceptedSize(1080, 1920) //单位px
            .setMediationAdSlot(  //测试广告位兜底对象
                MediationAdSlot.Builder()
                    // 将自定义兜底对象设置给 AdSlot
                    .setMediationSplashRequestInfo(csjSplashRequestInfo)
                    .build()
            )
            .build()
        adNativeLoader.loadSplashAd(adSlot, object : CSJSplashAdListener {
            override fun onSplashRenderSuccess(csjSplashAd: CSJSplashAd) {
                Log.e("qin","开屏渲染成功")
                /** 渲染成功后，展示广告  */
                mSplashAd=csjSplashAd;
                showSplashAd(csjSplashAd,container)
            }



            override fun onSplashLoadSuccess(p0: CSJSplashAd?) {
                mSplashAd=p0;
            }

            override fun onSplashLoadFail(csjAdError: CSJAdError) {
                /**仅CSJ
                 * csjAdError.getCode() == 1，物料加载失败
                 *
                 * csjAdError.getCode() == 2，素材加载失败
                 *
                 * csjAdError.getCode() == 3，渲染失败、渲染超时
                 *
                 * csjAdError.getCode() == 23，加载超时
                 *
                 */
                listener?.onError(csjAdError.msg)
            }

            override fun onSplashRenderFail(csjSplashAd: CSJSplashAd, csjAdError: CSJAdError) {
                Log.e("qin","开屏渲染失败"+csjAdError.msg)
                //listener.onError(csjAdError.code)
            }
        }, time.toInt())
    }
    private fun showSplashAd(csjSplashAd: CSJSplashAd, mSplashContainer: ViewGroup) {

        csjSplashAd.setSplashAdListener(object : CSJSplashAd.SplashAdListener {
            override fun onSplashAdShow(csjSplashAd: CSJSplashAd) {
                mSplashAd=csjSplashAd
                listener?.onAdShow(csjSplashAd, adUnitId = MyadUnitId,0)
            }

            override fun onSplashAdClick(csjSplashAd: CSJSplashAd) {
                // listener?.onAdClick()
            }

            override fun onSplashAdClose(csjSplashAd: CSJSplashAd, i: Int) {
                // 广告关闭后，销毁广告页面
                // listener?.onAdClose()
            }
        })
        val splashView = csjSplashAd.splashView

        mSplashContainer.removeAllViews()
        mSplashContainer.addView(splashView)

    }


    /**
     * 获取激励广告广告的ecpm
     */
    private fun getEcpm_Splash(ad: CSJSplashAd): String {
        val mediationManager: MediationBaseManager? = ad.mediationManager
        if (mediationManager != null) {
            val showEcpm = mediationManager.showEcpm
            if (showEcpm != null) {
                return showEcpm.ecpm
            }
        }
        return "0.0"
    }

    /**
     * 已经展示过且不再使用的广告需要在合适的时机调用destroy方法及时对广告进行销毁，避免内存异常场景。
     */
    fun onDestroy() {
        if (mSplashAd != null && mSplashAd?.getMediationManager() != null) {
            mSplashAd?.getMediationManager()?.destroy()
        }
    }
}